<template>
  <div>
    <page-header-wrapper>
      <application-search
        :filterFields="['SubmitToCity']"
        @search="handleSearch"
        @reset="handleReset"
      ></application-search>
      <a-card :bordered="false">
        <s-table
          ref="table"
          size="default"
          :rowKey="(record, index) => index"
          :changeExportQuery="true"
          :dataT="loadDataT"
          :dataG="loadDataG"
          :dataL="loadDataL"
          :columns="columns"
          :data="loadData"
          :scroll="{ x: true }"
        >
          <div slot="tabAction">
            <buttonSelect
              v-model="typeSelect"
              :options="[
                { value: '', label: '全部' },
                { value: '变更', label: '变更' },
                { value: '退出', label: '退出' },
                { value: '其他', label: '历史数据' },
                // { value: '2', label: '已接收' },
                // { value: '-1', label: '已退回' },
              ]"
              @change="tableRefresh(true)"
            ></buttonSelect>
          </div>
        </s-table>
      </a-card>
    </page-header-wrapper>
    <declare :data.sync="recordData" :visible.sync="visibleDeclare" />
  </div>
</template>

<script>
import { STable, Ellipsis, buttonSelect, applicationSearch } from '@/components'
import declare from './components/declare.vue'
import { getProposerList } from '@/api/communityReviewManagement'
import moment from 'moment'
export default {
  name: 'cityComplexInquireList',
  components: {
    STable,
    Ellipsis,
    buttonSelect,
    declare,
    applicationSearch,
  },
  computed: {
    changeColor() {
      return function (text) {
        if (text === '') {
          return 'text-success'
        }
        const classMap = new Map([
          ['0', 'text-success'],
          ['1', 'text-error'],
          ['-1', 'text-warning'],
        ])
        return classMap.get(text)
      }
    },
  },
  data() {
    return {
      visibleDeclare: false,
      typeSelect: '',
      queryParam: {
        UserID: this.$store.getters.userid,
        Mode: 'PrimaryAlter',
        strContent: {},
      },
      queryParamT: {
        UserID: this.$store.getters.userid,
        Mode: 'PrimaryAlter',
        strContent: {
          YDLX: '退出',
        },
      },
      queryParamG: {
        UserID: this.$store.getters.userid,
        Mode: 'PrimaryAlter',
        strContent: {
          YDLX: '变更',
        },
      },
      queryParamL: {
        UserID: this.$store.getters.userid,
        Mode: 'PrimaryAlter',
        strContent: {
          YDLX: '其他',
        },
      },
      recordData: {},
      columns: [
        {
          title: '序号',
          align: 'center',
          width: 80,
          customRender: (text, record, index) => <span>{index + 1}</span>,
        },
        {
          title: '申请编号',
          align: 'center',
          dataIndex: 'ApplyNo',
          width: 140,
        },
        {
          title: '申请人',
          align: 'center',
          dataIndex: 'Proposer',
          width: 140,
        },
        {
          title: '身份证号',
          align: 'center',
          dataIndex: 'IDCard',
          width: 180,
        },
        {
          title: '异动状态',
          align: 'center',
          width: 180,
          customRender: (text, record, index) => {
            const { QjState, SjState } = record
            const getText = () => {
              if (this.$isEmpty(QjState)) return '无异动'
              if (QjState === '0') {
                // 市级
                const sjMap = new Map([
                  ['0', '市级已执行，如需重新提交，请重新申报'],
                  ['1', '市级已作废，如需重新提交，请重新申报'],
                  ['-1', '市级审核中'],
                ])
                return sjMap.get(SjState)
              } else {
                // 区级
                const qjMap = new Map([
                  ['0', '区级已执行，如需重新提交，请重新申报'],
                  ['1', '区级已作废，如需重新提交，请重新申报'],
                  ['-1', '区级审核中'],
                ])
                return qjMap.get(QjState)
              }
            }
            return (
              <Ellipsis
                length={24}
                tooltip
                class={this.changeColor(record.QjState === '0' ? record.SjState : record.QjState)}
              >
                {getText()}
              </Ellipsis>
            )
          },
        },
        {
          title: '区/县',
          align: 'center',
          dataIndex: 'RegionName',
          width: 320,
        },
        {
          title: '街道/办事处',
          align: 'center',
          dataIndex: 'TownshipName',
          width: 180,
        },
        {
          title: '异动类型',
          align: 'center',
          dataIndex: 'YDLX',
        },
        {
          title: '现保障人口',
          align: 'center',
          dataIndex: 'XBZRK',
        },
        {
          title: '现人均住房面积',
          align: 'center',
          dataIndex: 'XRJZFMJ',
        },
        {
          title: '异动原因',
          align: 'center',
          dataIndex: 'ydReason',
        },
        {
          title: '审批时间',
          align: 'center',
          dataIndex: 'CityRfyDate',
          customRender: (text, record, index) => {
            return moment(text).format('YYYY-MM-DD HH:MM:SS')
          },
          width: 160,
        },
        // {
        //   title: '是否未执行的异动',
        //   align: 'center',
        //   dataIndex: 'SFWZX',
        //   width: 160,
        //   fixed: 'right',
        // },
        {
          title: '操作',
          align: 'center',
          width: 160,
          fixed: 'right',
          customRender: (text, record, index) => {
            // 市级审核日期小于15天
            if (moment().diff(moment(record.CityRfyDate), 'days') <= 15) {
              return (
                <a-tooltip>
                  <template slot="title">公示期未满，不可异动</template>
                  <a-icon type="lock" class="text-error" />
                </a-tooltip>
              )
            }
            const { QjState, SjState } = record
            if (QjState === '0') {
              // 市级
              if (SjState === '-1') {
                return <span class="text-warning">等待市级审核</span>
              }
              return (
                <y-button type="link" onclick={this.handleAudit.bind(this, record)}>
                  异动申报
                </y-button>
              )
            } else {
              return (
                <y-button type="link" onclick={this.handleAudit.bind(this, record)}>
                  {QjState !== '-1' ? '异动申报' : '异动修改'}
                </y-button>
              )
            }
          },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        requestParameters.strContent.YDLX = this.typeSelect
        return getProposerList(requestParameters).then((res) => res)
      },
      loadDataT: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParamT)
        // requestParameters.strContent.YDLX = this.typeSelect
        return getProposerList(requestParameters).then((res) => res)
      },
      loadDataG: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParamG)
        // requestParameters.strContent.YDLX = this.typeSelect
        return getProposerList(requestParameters).then((res) => res)
      },
      loadDataL: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParamL)
        // requestParameters.strContent.YDLX = this.typeSelect
        return getProposerList(requestParameters).then((res) => res)
      },
    }
  },
  methods: {
    handleSearch(values) {
      this.queryParam.strContent = values
      this.tableRefresh(true)
    },
    handleReset() {
      this.queryParam.strContent = {}
      this.typeSelect = ''
      this.tableRefresh(true)
    },
    tableRefresh(refresh = false) {
      this.$refs.table.refresh(refresh)
    },
    handleAudit(record) {
      this.visibleDeclare = true
      this.recordData = record
    },
  },
}
</script>
<style lang="less" scoped>
.table-header {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}
</style>
